<template>
  <div>
    <van-picker
      title="更改性别"
      show-toolbar
      :default-index="gender"
      :columns="columns"
      @confirm="onConfirm"
      @cancel="onCancel"
    />
  </div>
</template>

<script>
import { editUserProfileAPI } from '@/api'
export default {
  name: '',
  components: {},
  props: {
    gender: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      columns: ['男', '女']
    }
  },
  methods: {
    async onConfirm(value, index) {
      try {
        this.$toast.loading({
          message: '修改性别中....',
          forbidClick: true
        })
        // 1. 发请求
        await editUserProfileAPI({ gender: index })
        // 2. 修改视图
        this.$emit('update:gender', index)
        // 3. 关闭弹窗
        this.$emit('close')
        // 4. 给提示
        this.$toast.success('修改性别成功')
      } catch (error) {
        this.$toast.fail('修改性别失败')
      }
    },
    onCancel() {
      this.$emit('close')
    }
  },
  computed: {},
  watch: {},
  created() {}
}
</script>

<style scoped lang="less"></style>
